/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package util.sorting;



/**
 *
 * @author niko
 */

/**
 * Utility methods used throughout entire project.
 */
final class Tools
{
    private Tools()
    {
        // No instances.
    }

    /**
     * Check if all symbols in the given range are greater than 0, return
     * <code>true</code> if so, <code>false</code> otherwise.
     */
    static final boolean allPositive(int [] input, int start, int length)
    {
        for (int i = length - 1, index = start; i >= 0; i--, index++)
        {
            if (input[index] <= 0)
            {
                return false;
            }
        }

        return true;
    }

    /**
     * Determine the maximum value in a slice of an array.
     */
    static final int max(int [] input, int start, int length)
    {
        assert length >= 1;

        int max = input[start];
        for (int i = length - 2, index = start + 1; i >= 0; i--, index++)
        {
            final int v = input[index];
            if (v > max)
            {
                max = v;
            }
        }

        return max;
    }

    /**
     * Determine the minimum value in a slice of an array.
     */
    static final int min(int [] input, int start, int length)
    {
        assert length >= 1;

        int min = input[start];
        for (int i = length - 2, index = start + 1; i >= 0; i--, index++)
        {
            final int v = input[index];
            if (v < min)
            {
                min = v;
            }
        }

        return min;
    }

    /**
     * Calculate minimum and maximum value for a slice of an array.
     */
    static MinMax minmax(int [] input, final int start, final int length)
    {
        int max = input[start];
        int min = max;
        for (int i = length - 2, index = start + 1; i >= 0; i--, index++)
        {
            final int v = input[index];
            if (v > max)
            {
                max = v;
            }
            if (v < min)
            {
                min = v;
            }
        }

        return new MinMax(min, max);
    }

    /**
     * Throw {@link AssertionError} if a condition is <code>false</code>.
     */
    static final void assertAlways(boolean condition, String msg)
    {
        if (!condition)
        {
            throw new AssertionError(msg);
        }
    }
}
